/**
 * Filename: TimeUtils.java
 *
 * @author Firefly
 * @contact postfirefly@163.com
 * @time 2016/3/4 17:14
 */
package com.github.postsanf.yinian.utils;

import android.text.format.DateFormat;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;


public class TimeUtils {
    public static final long ONE_MINUTE_MILLIONS = 60 * 1000;
    public static final long ONE_HOUR_MILLIONS = 60 * ONE_MINUTE_MILLIONS;
    public static final long ONE_DAY_MILLIONS = 24 * ONE_HOUR_MILLIONS;

    public static String getOthershow(String dataStr, String fromStr, String toStr) {
        return dataStr.replace(fromStr, toStr);
    }

    public static String paserTimeToYN(long time) {
        System.setProperty("user.timezone", "Asia/Shanghai");
        TimeZone tz = TimeZone.getTimeZone("Asia/Shanghai");
        TimeZone.setDefault(tz);
        SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日");
        return format.format(new Date(time * 1000L));
    }

    public static String getShortTime(String dateStr) {
        String str = "";
        SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy", Locale.CHINA);
        try {
            Date date = sdf.parse(dateStr);
            Date curDate = new Date();
            long durTime = curDate.getTime() - date.getTime();
            int dayStatus = calculateDayStatus(date, curDate);
            if (durTime <= 10 * ONE_MINUTE_MILLIONS) {
                str = "刚刚";
            } else if (durTime < ONE_HOUR_MILLIONS) {
                str = durTime / ONE_MINUTE_MILLIONS + "分钟前";
            } else if (dayStatus == 0) {
                str = durTime / ONE_HOUR_MILLIONS + "小时前";
            } else if (dayStatus == -1) {
                str = "昨天" + DateFormat.format("HH:mm", date);
            } else if (isSameYear(date, curDate) && dayStatus < -1) {
                str = DateFormat.format("MM-dd", date).toString();
            } else {
                str = DateFormat.format("yyyy-MM", date).toString();
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return str;
    }

    //vivo手机使用此方法
    public static String getUpLoadTime(String euploadtime) {
        String str = "";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA);
        try {
            Date date = sdf.parse(euploadtime);
            Date curDate = new Date();

            long durTime = curDate.getTime() - date.getTime();
            int dayStatus = calculateDayStatus(date, curDate);

            if (durTime <= 10 * ONE_MINUTE_MILLIONS) {
                str = "刚刚";
            } else if (durTime < ONE_HOUR_MILLIONS) {
                str = durTime / ONE_MINUTE_MILLIONS + "分钟前";
            } else if (dayStatus == 0) {
                str = durTime / ONE_HOUR_MILLIONS + "小时前";
            } else if (dayStatus == -1) {
//				str = "昨天 " +DateFormat.format(" HH:mm", date) ;
                String[] split = euploadtime.split(" ");
                int i = split[1].lastIndexOf(":");
                String substring = split[1].substring(0, i);
                str = "昨天 " + substring;
            } else if (isSameYear(date, curDate) && dayStatus < -1) {
//				str = DateFormat.format("MM月dd日 HH:mm", date).toString();
                int m = euploadtime.indexOf("-");
                String substring = euploadtime.substring(m + 1, euploadtime.length());
                int i = substring.lastIndexOf(":");
                String split = substring.substring(0, i);
                str = split;
            } else {
//				str = DateFormat.format("yyyy年MM月dd日", date).toString();
                String[] split = euploadtime.split(" ");
                str = split[0];
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return str;
    }

    public static String getGroupStatusTime(String dateStr) {
        String str = "";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA);
        try {
            Date date = sdf.parse(dateStr);
            Date curDate = new Date();
            long durTime = curDate.getTime() - date.getTime();
            int dayStatus = calculateDayStatus(date, curDate);

            if (durTime <= 10 * ONE_MINUTE_MILLIONS) {
                str = "刚刚";
            } else if (durTime < ONE_HOUR_MILLIONS) {
                str = durTime / ONE_MINUTE_MILLIONS + "分钟前";
            } else if (dayStatus == 0) {
                str = durTime / ONE_HOUR_MILLIONS + "小时前";
            } else if (dayStatus == -1) {
                str = "昨天 " + DateFormat.format(" HH:mm", date);
            } else if (isSameYear(date, curDate) && dayStatus < -1) {
                str = DateFormat.format("MM月dd日 HH:mm", date).toString();
            } else {
                str = DateFormat.format("yyyy年MM月dd日", date).toString();
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return str;
    }

    public static String getMsgTime(String dateStr) {
        String str = "";

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA);
        try {
            Date date = sdf.parse(dateStr);
            Date curDate = new Date();

            long durTime = curDate.getTime() - date.getTime();
            int dayStatus = calculateDayStatus(date, curDate);

            if (durTime <= 10 * ONE_MINUTE_MILLIONS) {
                str = "刚刚";
            } else if (durTime < ONE_HOUR_MILLIONS) {
                str = durTime / ONE_MINUTE_MILLIONS + "分钟前";
            } else if (dayStatus == 0) {
                str = DateFormat.format(" HH:mm", date) + " 今天";
            } else if (dayStatus == -1) {
                str = DateFormat.format(" HH:mm", date) + " 昨天";
            } else if (isSameYear(date, curDate) && dayStatus < -1) {
                str = DateFormat.format("HH:mm  MM月dd日", date).toString();
            } else {
                str = DateFormat.format("yyyy年MM月dd日", date).toString();
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return str;
    }

    public static int getDayByTime(String dateStr) {
        int str = 0;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA);
        try {
            Date date = sdf.parse(dateStr);
            str = Integer.parseInt(DateFormat.format("dd", date).toString());
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return str;
    }

    public static String getMonthByTime(String dateStr) {
        String str = "";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA);
        try {
            Date date = sdf.parse(dateStr);
            str = numToMon(Integer.parseInt(DateFormat.format("MM", date).toString()));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return str;
    }

    public static String numToMon(int num) {
        String mon = "";
        switch (num) {
            case 1:
                mon = "一月";
                break;
            case 2:
                mon = "二月";
                break;
            case 3:
                mon = "三月";
                break;
            case 4:
                mon = "四月";
                break;
            case 5:
                mon = "五月";
                break;
            case 6:
                mon = "六月";
                break;
            case 7:
                mon = "七月";
                break;
            case 8:
                mon = "八月";
                break;
            case 9:
                mon = "九月";
                break;
            case 10:
                mon = "十月";
                break;
            case 11:
                mon = "十一月";
                break;
            case 12:
                mon = "十二月";
                break;
        }
        return mon;
    }

    public static String getTDayByTime(String dateStr) {
        String str = "";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA);
        try {
            Date date = sdf.parse(dateStr);
            str = Integer.parseInt(DateFormat.format("dd", date).toString()) + "";
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return str;
    }

    public static String getStatusTime(String dateStr) {
        String str = "";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA);
        try {
            Date date = sdf.parse(dateStr);
            Date curDate = new Date();
            long durTime = curDate.getTime() - date.getTime();
            int dayStatus = calculateDayStatus(date, curDate);
            if (durTime <= 10 * ONE_MINUTE_MILLIONS) {
                str = "刚刚";
            } else if (durTime < ONE_HOUR_MILLIONS) {
                str = durTime / ONE_MINUTE_MILLIONS + "分钟前";
            } else if (dayStatus == 0) {
                str = durTime / ONE_HOUR_MILLIONS + "小时前";
            } else if (dayStatus == -1) {
                str = "昨天";
            } else if (isSameYear(date, curDate) && dayStatus < -1) {
                str = DateFormat.format("MM月dd日", date).toString();
            } else {
                str = DateFormat.format("yyyy年MM月dd日", date).toString();
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return str;
    }


    public static String getDetailedTime(String dateStr) {
        String str = "";

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA);
        try {
            Date date = sdf.parse(dateStr);
            Date curDate = new Date();

            long durTime = curDate.getTime() - date.getTime();
            int dayStatus = calculateDayStatus(date, curDate);
            // 今天 返回空   昨天及以前 返回 时间
            if (dayStatus == 0 || durTime <= ONE_HOUR_MILLIONS) {
                str = "";
            } else if (dayStatus <= -1) {
                str = DateFormat.format("HH:mm", date).toString();
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return str;
    }


    public static boolean isSameYear(Date targetTime, Date compareTime) {
        Calendar tarCalendar = Calendar.getInstance();
        tarCalendar.setTime(targetTime);
        int tarYear = tarCalendar.get(Calendar.YEAR);

        Calendar compareCalendar = Calendar.getInstance();
        compareCalendar.setTime(compareTime);
        int comYear = compareCalendar.get(Calendar.YEAR);

        return tarYear == comYear;
    }

    public static int calculateDayStatus(Date targetTime, Date compareTime) {
        Calendar tarCalendar = Calendar.getInstance();
        tarCalendar.setTime(targetTime);
        int tarDayOfYear = tarCalendar.get(Calendar.DAY_OF_YEAR);

        Calendar compareCalendar = Calendar.getInstance();
        compareCalendar.setTime(compareTime);
        int comDayOfYear = compareCalendar.get(Calendar.DAY_OF_YEAR);

        return tarDayOfYear - comDayOfYear;
    }

    public static String secToTime(int time) {
        String timeStr = null;
        int hour = 0;
        int minute = 0;
        int second = 0;
        if (time <= 0)
            return "00:00:00";
        else {
            minute = time / 60;
            if (minute < 60) {
                second = time % 60;
                timeStr = "00:" + unitFormat(minute) + ":" + unitFormat(second);
            } else {
                hour = minute / 60;
                if (hour > 99)
                    return "99:59:59";
                minute = minute % 60;
                second = time - hour * 3600 - minute * 60;
                timeStr = unitFormat(hour) + ":" + unitFormat(minute) + ":" + unitFormat(second);
            }
        }
        return timeStr;
    }

    public static String unitFormat(int i) {
        String retStr = null;
        if (i >= 0 && i < 10)
            retStr = "0" + Integer.toString(i);
        else
            retStr = "" + i;
        return retStr;
    }


    public static String getNowWeek() {
        final Calendar c = Calendar.getInstance();
        c.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
        String mWay = String.valueOf(c.get(Calendar.DAY_OF_WEEK));
        if ("1".equals(mWay)) {
            mWay = "天";
        } else if ("2".equals(mWay)) {
            mWay = "一";
        } else if ("3".equals(mWay)) {
            mWay = "二";
        } else if ("4".equals(mWay)) {
            mWay = "三";
        } else if ("5".equals(mWay)) {
            mWay = "四";
        } else if ("6".equals(mWay)) {
            mWay = "五";
        } else if ("7".equals(mWay)) {
            mWay = "六";
        }
        return "星期" + mWay;
    }

    public static String getNowDay() {
        final Calendar c = Calendar.getInstance();
        c.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
        String mDay = String.valueOf(c.get(Calendar.DAY_OF_MONTH));// 获取当前月份的日期号码
        return mDay;
    }

    public static String getNowYearMouth() {
        final Calendar c = Calendar.getInstance();
        c.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
        String mYear = String.valueOf(c.get(Calendar.YEAR)); // 获取当前年份
        String mMonth = String.valueOf(c.get(Calendar.MONTH) + 1);// 获取当前月份
        return mYear + "/" + mMonth;
    }

}
